package com.simon.spark.sql

import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

object SqlTest {
  def main(args: Array[String]): Unit = {

    //创建SparkConf()并设置App名称
    val conf = new SparkConf().setAppName("SQL-1").setMaster("local[2]")
    //SQLContext要依赖SparkContext
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc);
    val personRDD = sc.textFile("e://persons.txt").map(_.split(" ")).map(x => Person(x(0).toInt, x(1), x(2).toInt))

    import sqlContext.implicits._
    val personDF = personRDD.toDF
    personDF.registerTempTable("t_person")


    val df = sqlContext.sql("select * from t_person order by age desc limit 3")

    df.write.json("e://persons.json")

    sc.stop();
  }
}

